<?php ob_start(); 
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php $message = ""; ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

// Vérifier si un même code existe déjà dans la table conditionvente
if(isset($_POST['Code_condition_vente'])){
	// meme code et meme valeur  --  THIA
    // --- si le code existe pour l'offre
$toCriterias['Valeur_condition_vente'] = $_POST['Valeur_condition_vente'] ;
$toCriterias['Code_condition_vente'] = $_POST['Code_condition_vente'] ;
$toCriterias['Identifiant_offre'] = $_POST['Identifiant_offre'] ;
$existeOffre = existeEnregistrement($database_microassur, $microassur, "conditionvente", $toCriterias) ;

// --- si le code existe pour le produit
$toCriterias['Identifiant_produit'] = $_POST['Identifiant_produit'] ;
unset($toCriterias['Identifiant_offre']) ;
$existeProduit = existeEnregistrement($database_microassur, $microassur, "conditionvente", $toCriterias) ;

$existe = $existeOffre && $existeProduit ;  

	// $existe = existeDeuxChamp($database_microassur, $microassur, 'conditionvente', 'Code_condition_vente', 'Valeur_condition_vente', @$_POST['Code_condition_vente'], @$_POST['Valeur_condition_vente']);
	$verif=veriflibelle1("Code_condition_vente");
}
// Contrôle de saisie des Dates DEBUT/FIN
if(isset($_POST['Date_fin_condition_vente']) && $_POST['Date_debut_condition_vente']){
	$datedebut = datevalider($_POST['Date_debut_condition_vente']);
		$datefin = datevalider($_POST['Date_fin_condition_vente']);
	$datevalide1 = datevalide($_POST['Date_debut_condition_vente']);
		$datevalide2 = datevalide($_POST['Date_fin_condition_vente']);
	$lesdates=verifDate($_POST['Date_debut_condition_vente'],$_POST['Date_fin_condition_vente']);
}
if(isset($_POST['Code_condition_vente']) && empty($_POST['Code_condition_vente'])){
	$message = colorer("Vous devez saisir le code","false");
}elseif(isset($_POST['Code_condition_vente']) && isset($_POST['Valeur_condition_vente']) && isset($_POST['Identifiant_offre']) && $existeOffre!=0){
	$message = colorer("Cette condition de vente existe déjà pour l'offre choisie","false");
}elseif(isset($_POST['Code_condition_vente']) && isset($_POST['Valeur_condition_vente']) && isset($_POST['Identifiant_produit']) && $existeProduit!=0){
	$message = colorer("Cette condition de vente existe déjà pour le produit choisi","false");
}elseif(isset($_POST['Code_condition_vente']) && !$verif){
	$message = colorer("Le code saisi comporte des caractères spéciaux interdits","false");
}elseif(isset($_POST['Libelle_condition_vente']) && empty($_POST['Libelle_condition_vente'])){
	$message = colorer(" Vous devez saisir le libellé","false");
}/*elseif(isset($_POST['Valeur_condition_vente']) && !empty($_POST['Valeur_condition_vente']) &&  !verifNumeric($_POST['Valeur_condition_vente'])){
	$message = colorer("La valeur de la condition de vente doit être numérique","false");
}*/elseif(isset($_POST['Date_debut_condition_vente']) && empty($_POST['Date_debut_condition_vente'])){
	$message = colorer("Vous devez saisir la date de début","false");
}elseif(isset($_POST['Date_debut_condition_vente']) && !$datedebut){
	$message = colorer("Le format de la date de début est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_debut_condition_vente']) && !$datevalide1){
	$message = colorer("La date de début est incorrecte ","false");
}elseif(isset($_POST['Date_fin_condition_vente']) && $_POST['Date_fin_condition_vente']!=NULL && !$datefin){
	$message = colorer("Le format de la date de fin est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_fin_condition_vente']) && $_POST['Date_fin_condition_vente']!=NULL && !$datevalide2){
	$message = colorer("La date de fin est incorrecte ","false");
}elseif(isset($_POST['Date_debut_condition_vente']) && isset($_POST['Date_fin_condition_vente'])  && !empty($_POST['Date_fin_condition_vente']) && !empty($_POST['Date_debut_condition_vente'])&& $lesdates==FALSE){
	$message = colorer("La date de fin doit être postérieure à la date de début","false");
}
else{
	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $existe==0) {
	  $insertSQL = sprintf("INSERT INTO conditionvente (Identifiant_condition_vente, Code_condition_vente, Libelle_condition_vente, Valeur_condition_vente, Type_condition_vente, Date_debut_condition_vente, Date_fin_condition_vente, Identifiant_offre, Identifiant_produit) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
						   GetSQLValueString($_POST['Identifiant_condition_vente'], "int"),
						   GetSQLValueString($_POST['Code_condition_vente'], "text"),
						   GetSQLValueString($_POST['Libelle_condition_vente'], "text"),
						   GetSQLValueString($_POST['Valeur_condition_vente'], "int"),
						   GetSQLValueString($_POST['Type_condition_vente'], "text"),
						   GetSQLValueString(dateEN2FR($_POST['Date_debut_condition_vente']), "date"),
						   GetSQLValueString(dateEN2FR($_POST['Date_fin_condition_vente']), "date"),
						   GetSQLValueString($_POST['Identifiant_offre'], "int"),
						   GetSQLValueString($_POST['Identifiant_produit'], "int"));
	
	  mysql_select_db($database_microassur, $microassur);
	   mysql_query("START TRANSACTION");

	  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
	  
	  if($Result1) 
{ mysql_query("commit");
$message=colorer("La condition de vente est créée","true");
			unset($_POST); 
}
else { mysql_query("rollback"); $message = colorer("Contacter la DSI, erreur création de le condition de vente","false");}

	
	}
}

$maxRows_rs_list = 10;
$pageNum_rs_list = 0;
if (isset($_GET['pageNum_rs_list'])) {
  $pageNum_rs_list = $_GET['pageNum_rs_list'];
}
$startRow_rs_list = $pageNum_rs_list * $maxRows_rs_list;

mysql_select_db($database_microassur, $microassur);
$query_rs_list = "SELECT * FROM conditionvente";
//$query_limit_rs_list = sprintf("%s LIMIT %d, %d", $query_rs_list, $startRow_rs_list, $maxRows_rs_list);
$query_limit_rs_list = $query_rs_list;
$rs_list = mysql_query($query_limit_rs_list, $microassur) or die(mysql_error());
$row_rs_list = mysql_fetch_assoc($rs_list);

if (isset($_GET['totalRows_rs_list'])) {
  $totalRows_rs_list = $_GET['totalRows_rs_list'];
} else {
  $all_rs_list = mysql_query($query_rs_list);
  $totalRows_rs_list = mysql_num_rows($all_rs_list);
}
$totalPages_rs_list = ceil($totalRows_rs_list/$maxRows_rs_list)-1;

mysql_select_db($database_microassur, $microassur);
$query_rs_offre = "SELECT * FROM offre";
$rs_offre = mysql_query($query_rs_offre, $microassur) or die(mysql_error());
$row_rs_offre = mysql_fetch_assoc($rs_offre);
$totalRows_rs_offre = mysql_num_rows($rs_offre);

mysql_select_db($database_microassur, $microassur);
$query_rs_produit = "SELECT * FROM produit";
$rs_produit = mysql_query($query_rs_produit, $microassur) or die(mysql_error());
$row_rs_produit = mysql_fetch_assoc($rs_produit);
$totalRows_rs_produit = mysql_num_rows($rs_produit);

$queryString_rs_list = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rs_list") == false && 
        stristr($param, "totalRows_rs_list") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rs_list = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rs_list = sprintf("&totalRows_rs_list=%d%s", $totalRows_rs_list, $queryString_rs_list);

/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="T0002"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../condvente.css.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../form_ie7.css" /><![endif]-->
            <link type="text/css" href="../../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../../js/scripts.js"></script>
	</head>
<body id="cdv">
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Condition vente";
					$nommodule2="Liste conditions vente";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
					<table class="tab_list">
						  <tr id="niv1">
							<td class="t1">CODE</td>
							<td class="t2">LIBELLE</td>
							<td class="t2">DATE DEBUT/FIN</td>
							<td class="t1">VALEUR</td>
							<td class="t3">TYPE</td>
							<td class="t4">OFFRE</td>
							<td>PRODUIT</td>
						  </tr>
					</table>
				<div class="content_table">
					<table class="tab_list ns0">
							<?php $i=1; do 	{
								mysql_select_db($database_microassur, $microassur);
								$query_rs_lab_offre = "SELECT * FROM offre WHERE Identifiant_offre ='". $row_rs_list['Identifiant_offre']."'";
								$rs_lab_offre = mysql_query($query_rs_lab_offre, $microassur) or die(mysql_error());
								$row_rs_lab_offre = mysql_fetch_assoc($rs_lab_offre);
								$totalRows_rs_lab_offre = mysql_num_rows($rs_lab_offre);

								mysql_select_db($database_microassur, $microassur);
								$query_rs_lab_produit = "SELECT * FROM produit WHERE Identifiant_produit ='". $row_rs_list['Identifiant_produit'] ."'";
								$rs_lab_produit = mysql_query($query_rs_lab_produit, $microassur) or die(mysql_error());
								$row_rs_lab_produit = mysql_fetch_assoc($rs_lab_produit);
								$totalRows_rs_lab_produit = mysql_num_rows($rs_lab_produit);
								
							  ?>
							  <?php if ($totalRows_rs_list > 0) { // Show if recordset not empty ?>
								<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
								  <td class="t1"><a href="modconditionvente.php?Identifiant_condition_vente=<?php echo $row_rs_list['Identifiant_condition_vente']; ?>"><?php echo $row_rs_list['Code_condition_vente']; ?></a></td>
								  <td class="t2"><a href="modconditionvente.php?Identifiant_condition_vente=<?php echo $row_rs_list['Identifiant_condition_vente']; ?>"><?php echo $row_rs_list['Libelle_condition_vente']; ?></a></td>
								  <td class="t2"><a href="modconditionvente.php?Identifiant_condition_vente=<?php echo $row_rs_list['Identifiant_condition_vente']; ?>"><?php echo dateEN2FR($row_rs_list['Date_debut_condition_vente']) . " / " ?><?php if($row_rs_list['Date_fin_condition_vente']!=NULL) echo dateEN2FR($row_rs_list['Date_fin_condition_vente']); else echo $indefini; ?></a></td>
								  <td class="t1"><a href="modconditionvente.php?Identifiant_condition_vente=<?php echo $row_rs_list['Identifiant_condition_vente']; ?>"><?php echo $row_rs_list['Valeur_condition_vente']; ?></a></td>
								  <td class="t3"><a href="modconditionvente.php?Identifiant_condition_vente=<?php echo $row_rs_list['Identifiant_condition_vente']; ?>"><?php echo $row_rs_list['Type_condition_vente']; ?></a></td>
								  <td class="t4"><a href="modconditionvente.php?Identifiant_condition_vente=<?php echo $row_rs_list['Identifiant_condition_vente']; ?>"><?php echo $row_rs_lab_offre['Libelle_offre']; ?></a></td>
								  <td><a href="modconditionvente.php?Identifiant_condition_vente=<?php echo $row_rs_list['Identifiant_condition_vente']; ?>"><?php echo $row_rs_lab_produit['Libelle_produit']; ?></a></td>
								</tr>
								<?php } // Show if recordset not empty ?>

							  <?php $i++; } while ($row_rs_list = mysql_fetch_assoc($rs_list)); ?>
					</table>
				</div>
			</div>
		<div class="imgright"></div>
			<div class="cleared"></div>
		<!--/***************************************/-->
			<div id="br"></div>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
				<div class="imgmiddle">
					<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
					<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
						<div class="content_tableform1">
						  <table>
							<tr>
							  <td>CODE</td>
							  <td><input class="resize_1" type="text" name="Code_condition_vente" value="<?php echo remind_text('Code_condition_vente'); ?>" size="32" maxlength="5" /></td>
							</tr>
							<tr>
							  <td>LIBELLE</td>
							  <td colspan="3"><input class="td2" type="text" name="Libelle_condition_vente" value="<?php echo remind_text('Libelle_condition_vente'); ?>" size="32" maxlength="30" /></td>
							</tr>
							<tr>
							  <td>VALEUR</td>
							  <td colspan="3"><input class="td1" type="text" name="Valeur_condition_vente" value="<?php echo remind_text('Valeur_condition_vente'); ?>" size="32" maxlength="10" /></td>
							</tr>
							<tr>
							  <td>TYPE</td>
							  <td colspan="3">
								<select class="select" name="Type_condition_vente">
								<?php do{ ?>
											<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php echo remind_select('Type_condition_vente', $row_rs_dico2['Code_element']) ?> ><?php echo $row_rs_dico2['Nom_element']; ?></option>
								<?php 	} while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
								</select>
							  </td>
							</tr>
							<tr>
							  <td>DATE DEBUT </td>
							  <td><input class="date" type="text" name="Date_debut_condition_vente" value="<?php echo remind_text('Date_debut_condition_vente'); ?>" size="10" maxlength="10" /> </td>
							  <td class="td">DATE FIN</td>
							  <td><input class="date" type="text" name="Date_fin_condition_vente" value="<?php echo remind_text('Date_fin_condition_vente'); ?>" size="10" maxlength="10" /> </td>
							</tr>
							<tr>
							  <td>OFFRE</td>
							  <td>
							  <select class="resize_1" name="Identifiant_offre">
								<?php
						do {  
						?><option value="<?php echo $row_rs_offre['Identifiant_offre']?>"><?php echo $row_rs_offre['Libelle_offre']?></option>
								<?php
						} while ($row_rs_offre = mysql_fetch_assoc($rs_offre));
						  $rows = mysql_num_rows($rs_offre);
						  if($rows > 0) {
							  mysql_data_seek($rs_offre, 0);
							  $row_rs_offre = mysql_fetch_assoc($rs_offre);
						  }
						?>
							  </select>
							  </td>
							  <td class="td">PRODUIT</td>
							  <td><select class="resize_1" name="Identifiant_produit"> 
							  <option value="">Sélectionner un produit</option>
								<?php
						do {  
						?>
								<option value="<?php echo $row_rs_produit['Identifiant_produit']?>" <?php echo remind_select('Identifiant_produit', $row_rs_produit['Identifiant_produit']) ?> ><?php echo $row_rs_produit['Libelle_produit']?></option>
								<?php
						} while ($row_rs_produit = mysql_fetch_assoc($rs_produit));
						?>
							  </select>
							  </td>
							</tr>
							  </table>
							</div>
					</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
			<input type="hidden" name="Identifiant_condition_vente" value="" />
			<input type="hidden" name="MM_insert" value="form1" />
</form>
		</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs_list);
mysql_free_result($rs_offre);
mysql_free_result($rs_produit);
mysql_free_result($rs_lab_offre);
mysql_free_result($rs_lab_produit);
?>